//
//  RootViewController.m
//  UISearchBar视频
//
//  Created by dllo on 16/1/5.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "RootViewController.h"
// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width

@interface RootViewController () <UISearchBarDelegate>

@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation RootViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 50, WIDTH, 0)];
    [self.view addSubview:search];
    [search release];
    
    search.placeholder = @"search";
    search.prompt = @"蓝欧";
//    search.text = @"lanou";
    
    
    search.barStyle = UIBarStyleDefault;
    search.tintColor = [UIColor cyanColor];
    search.translucent = YES;
    
    // 自动大小写类型
    search.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    // 弹出键盘类型
    search.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    // button 书签
//    search.showsBookmarkButton = YES;
//    search.showsCancelButton = YES;
//    search.showsSearchResultsButton = YES;
    // 动画效果
//    [search setShowsCancelButton:YES];
    
    // 范围的button
    search.showsScopeBar = YES;
    
    search.scopeButtonTitles = @[@"你好", @"呵呵", @"嘻嘻"];
    // 默认选中哪个button
    search.selectedScopeButtonIndex = 2;
    
//    // 设置背景图
//    [search setBackgroundImage:[UIImage imageNamed:@"1.jpg"]];
//    // 设置scopebar的背景图
//    [search setScopeBarBackgroundImage:[UIImage imageNamed:@"1.jpg"]];
//    
//    // searchbutton 的图片
//    [search setImage:[UIImage imageNamed:@"1.jpg"] forSearchBarIcon:(UISearchBarIconResultsList) state:(UIControlStateNormal)];
//    
    search.delegate = self;
    
    self.dataArray = [NSMutableArray arrayWithObjects:@"class 1", @"class2", @"class3", @"class4", @"class5", nil];
    
}

// 进入编辑状态
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    // 显示cancel
    [searchBar setShowsCancelButton:YES];
    
}
// 结束编辑状态
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    
    return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    
}
// 记录输入的文字
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    
    NSLog(@"%@", searchText);
    
    // 创建谓词  谓词中包含筛选语句,某个对象是否包含这个字符串
    // 谓词的用途: 1. if语句的条件 2.数据筛选
    // c 不区分大小写
    // d 不区分重音
    // 判断是否以这个输入开头
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self beginswith [cd] %@", searchText];
    // 判断是否包含
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@", searchText];

    // 判断的对象  123中是否包含你的输入
    BOOL result = [predicate evaluateWithObject:@"123"];
    NSLog(@"result = %@", result ? @"yes" : @"no");
    
    
}
// 不打开搜索上面就不可以打字
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0){
    
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    NSLog(@"%s", __FUNCTION__);
    
}// called when keyboard search button pressed
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar{
    
    NSLog(@"%s", __FUNCTION__);

    
}// called when bookmark button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    // 点击cancel按钮 之后让cancel 消失
    [searchBar setShowsCancelButton:NO animated:YES];
    
    // 让搜索框取消第一响应
    [searchBar resignFirstResponder];
    
    
    NSLog(@"%s", __FUNCTION__);

    
}// called when cancel button pressed
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2){
    
    NSLog(@"%s", __FUNCTION__);

    
}// called when search results button pressed

// 监控点击的是哪个button
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0){
    NSLog(@"%s", __FUNCTION__);
    
    NSLog(@"dfsfds%ld", (long)selectedScope);

 
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
